﻿import clr
from SSMSCore import *
from SSMSUtilityFunctions import *
from System.Windows.Forms import *

from System import *
from System.Collections import *
from System.Collections.Generic import *

clr.AddReferenceToFileAndPath(r"ProcedureList.Library.dll")
from ProcedureList.Library.UI import *
from ProcedureList.Library.Domain import *

try:
    from DocumentScanner import *
except System.Exception, e:
    print "Error importing DocumentScanner:" + repr(e)

def GetDto(methodName):
    dto = MethodDto()
    dto.MethodName = methodName
    dto.ParentClassName = methodName
    return dto

class ProcedureListCommand(CommandHandler):
    
    def AddMember(self, list, generator, prefix):
        for item in generator():
            dto = MethodDto()
            dto.MethodName = item.Text
            dto.ParentClassName = prefix
            dto.Position = item.LineNumber
            list.Add(dto)
    
    def AddCustomMethods(self, list, documentScanner):
        try:
            expressions = Path.Combine(self.GetWorkingDirectory(), "Scripts\Expressions.txt")
            re = Regex(r"([\w]+),(.*)")
            if File.Exists(expressions):
                for line in ReadLines(expressions):
                    m = re.Match(line)
                    if m.Success:
                        self.AddMember(
                            list, 
                            lambda: documentScanner.MatchAbsolute(m.Groups[2].ToString(), documentScanner.GetEvaluatorFirstGroup()),
                            m.Groups[1].ToString())
                        
        except System.Exception, e:
            InspectWithPyPad(globals(), locals())
            self.ShowMessage("Error in populateCustomGroups: %s" % repr(e))            
    
    
    def AddMethods(self, list, scanner):  
        
        self.AddMember(list, scanner.GetTempTables, "#TEMP")
        self.AddMember(list, scanner.GetRegions, "#REGION")
        self.AddMember(list, scanner.GetVariables, "@VARIABLE")
        self.AddMember(list, scanner.GetIndexes, "IDX")
        
    def isTextWindow(self, window):
        return window and window.Selection and (window.Caption <> "SQL Navigator")
            
    def InternalExecute(self, ExecuteOption, VariantIn, VariantOut, Handled):
        activeWindow = self._DTE.ActiveWindow
        if self.isTextWindow(activeWindow):        
            print "MyCommand Executed"
            frm = FormProcedureList()
            
            savedOffset = activeWindow.Selection.ActivePoint.AbsoluteCharOffset             
            activeWindow.Selection.SelectAll()
            text = activeWindow.Selection.Text 
            activeWindow.Selection.Cancel()
            activeWindow.Selection.MoveToAbsoluteOffset(savedOffset)        
            scanner = DocumentScanner(text)
            dtos = List[MethodDto]()
            self.AddMethods(dtos, scanner)
            self.AddCustomMethods(dtos, scanner)
            frm.BindMethods(dtos)

            frm.ShowDialog()
            if frm.SelectedMethod:                 
                 activeWindow.Selection.GotoLine(frm.SelectedMethod.Position)
